package org.paradise.dms.web.action.maintainreport;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.paradise.dms.pojo.MaintainReport;
import org.paradise.dms.pojo.MaintainReportStatus;
import org.paradise.dms.services.MainReportService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.Pager;
import org.paradise.dms.web.tools.PagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description:
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 Mar 18, 2009 9:03:26 PM 郑旭（zhengxu2006@gmail.com）created
 * @version 1.1 Mar 19, 2009 9:21:26 AM 李萌（neolimeng@gmail.com) modified
 */
@Service
public class MaintainReportAction extends DMSBaseAction {

	private static Logger log = Logger.getLogger(MaintainReportAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -7859390654711841354L;

	@Autowired
	@Qualifier("mainReportServiceImpl")
	private MainReportService maintainreportservice;
	private MaintainReport mr;
	private String submit; // submitButton的值
	private List<MaintainReport> mrlist = null;

	@Autowired
	private PagerService pagerService;
	private Pager pager;
	protected String currentPage;
	protected String totalRows;
	protected String pagerMethod;

	/**
	 * 
	 * Description: 增加一条维修记录。
	 * 
	 * @Version1.0 Mar 20, 2009 11:58:31 AM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String maintainreportinsert() {
		mr.setMaintainreportind(1);
		mr.setMaintainreporttime(new Date());
		MaintainReportStatus mrsstatus = new MaintainReportStatus();
		mrsstatus.setMaintainreportstatusid(1);
		mr.setMaintainreportstatus(mrsstatus);
		if (this.maintainreportservice.insertMainReport(this.mr)) {
			return SUCCESS;
		} else
			return INPUT;
	}

	/**
	 * 
	 * Description: 根据MainReport对象更新一条维修记录
	 * 
	 * @Version1.0 Mar 20, 2009 7:49:49 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @return
	 */
	public String maintainreportupdate() {
		mr.setMaintainreportind(1);
		boolean flag = maintainreportservice.updateMainReport(this.mr);
		if (flag) {
			return SUCCESS;
		}
		return INPUT;
	}

	/**
	 * 
	 * Description: 根据maintainreportid取得一条维修记录
	 * 
	 * @Version1.0 Mar 20, 2009 11:57:54 AM 李萌（neolimeng@gmail.com）创建
	 * @return
	 */
	public String findMRByMaintainReportID() throws DaoException {
		int mrid = Integer.parseInt(ServletActionContext.getRequest().getParameter("maintainreportid"));
		List<MaintainReport> lst = maintainreportservice.findMainReport(mrid);
		if (lst != null && lst.size() > 0) {
			mr = lst.get(0);
		}
		return SUCCESS;
	}

	/**
	 * 
	 * Description:获取所有的维修列表记录
	 * 
	 * @Version1.0 2009-3-20 下午02:58:37 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 * @throws DaoException
	 */
	public String getAllMaintainReport() throws DaoException {
		mrlist = maintainreportservice.findAllMainReports();
		if (mrlist != null && mrlist.size() > 0) {
			return SUCCESS;
		}
		return INPUT;
	}

	/**
	 * 
	 * Description: 分页查询maintainreport所有记录
	 * 
	 * @Version1.0 Apr 30, 2009 12:45:10 AM 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public String listAllMaintainReport() {
		try {
			int totalRow = this.maintainreportservice.getAllRows(); // 这里在service层写一个查询所有结果ROWS的方法。
			pager = pagerService.getPager(this.getCurrentPage(), this.getPagerMethod(), totalRow);
			this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
			this.setTotalRows(String.valueOf(totalRow));
			mrlist = this.maintainreportservice.listAllMaintainReports(pager.getPageSize(), pager.getStartRow());
			return SUCCESS;
		} catch (Exception e) {
			log.info("DMS_error:根据姓名查询所有维修记录有误！");
			log.info("DMS_error:错误原因！" + e);
			return INPUT;
		}
	}

	/**
	 * 
	 * Description: 根据maintainreportid删除一条记录
	 * 
	 * @Version1.0 Mar 20, 2009 9:15:33 PM 郑旭（zhengxu2006@gmail.com）创建
	 * @return
	 */
	public String maintainreportdelete() {
		int mrid = Integer.parseInt(ServletActionContext.getRequest().getParameter("maintainreportid"));
		if (maintainreportservice.deleteMainReport(mrid))
			return SUCCESS;
		else
			return INPUT;
	}

	public String setFinished() {
		int mrid = Integer.parseInt(ServletActionContext.getRequest().getParameter("maintainreportid"));
		if (this.maintainreportservice.setFinished(mrid)) {
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	public MainReportService getMaintainreportservice() {
		return maintainreportservice;
	}

	public void setMaintainreportservice(MainReportService maintainreportservice) {
		this.maintainreportservice = maintainreportservice;
	}

	public MaintainReport getMr() {
		return mr;
	}

	public void setMr(MaintainReport mr) {
		this.mr = mr;
	}

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	public List<MaintainReport> getMrlist() {
		return mrlist;
	}

	public void setMrlist(List<MaintainReport> mrlist) {
		this.mrlist = mrlist;
	}

	public PagerService getPagerService() {
		return pagerService;
	}

	public void setPagerService(PagerService pagerService) {
		this.pagerService = pagerService;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public String getTotalRows() {
		return totalRows;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public String getPagerMethod() {
		return pagerMethod;
	}

	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}

}
